package com.dyz.leetcode._12_linkedList;

import com.dyz.leetcode.ListNode;

public class _2_addTwoNumbers {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int carry = 0;
        ListNode dummy = new ListNode(-1);
        ListNode curr = dummy;
        while(l1!=null || l2!=null){
            int x = l1==null?0:l1.val;
            l1 = l1==null?null:l1.next;
            int y = l2==null?0:l2.val;
            l2 = l2==null?null:l2.next;

            int sum = x+y+carry;
            carry = sum/10;
            curr.next = new ListNode(sum%10);
            curr = curr.next;
        }
        //  最后一位
        if(carry!=0){
            curr.next = new ListNode(carry);
        }
        return dummy.next;
    }
}
